package com.webbei.custom.action;

import java.io.File; 
import java.io.IOException; 
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils; 
import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionSupport;

public class PicUpload extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File myfile;
	private String myfileContentType;
	private String myfileFileName;
	private String picname;
	public void setMyfile(File myfile) {
		this.myfile = myfile;
	}
	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}
	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}
	public String getPicname() {
		return picname;
	}
	public String execute(){ 
		String path = ServletActionContext.getServletContext().getRealPath("/temp"); 

        String[] t = myfileContentType.split("/"); 
        picname = String.valueOf((new Date().getTime()))+String.valueOf(new Random().nextInt(3))+
        		"."+t[t.length-1];
        if(myfile != null){ 
        File savefile = new File(new File(path),picname); 
        picname = "temp/" + picname;
        if(!savefile.getParentFile().exists()) 
            savefile.getParentFile().mkdirs(); 
        try { 
            FileUtils.copyFile(myfile , savefile); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block  
            e.printStackTrace(); 
        } 
          
        } 
       
		
		return SUCCESS;
	}
	
	
}
